package com.qianbao.qianbaobusiness.ui.cashier.databill;

import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.baidu.tts.tools.SharedPreferencesUtils;
import com.baseandroid.utils.FormatUtil;
import com.qianbao.qianbaobusiness.R;


/**
 * 开发者：LuoYi
 * Time: 2017 10:41 2017/4/25 04
 */

public abstract class BaseScrollviewFreshActivity extends BaseToolBarActivity {
    protected PullableScrollView scrollView;
    protected PullToRefreshLayout refreshLayout;
    protected FrameLayout emptyLayout;
    private boolean isRefresh = true;
    private View tileView;

    @Override
    protected void init() {
        setContentView(R.layout.layout_scrollview);
        refreshLayout = (PullToRefreshLayout) findViewById(R.id.refresh_view);
        scrollView = (PullableScrollView) findViewById(R.id.content_view);
        emptyLayout = (FrameLayout) findViewById(R.id.layout_empty);
        tileView = (View) findViewById(R.id.line_title_view);

        refreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                isRefresh = true;
//                if (isNetworkAvailable(getThis()) == 0) {
//                    ToastUtil.show(getThis(), getResources().getString(R.string.toast_network_failure_default), 1000);
//                    stopLoad(false);
//                    return;
//                }
                onRefreshFinish();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                isRefresh = false;
//                if (isNetworkAvailable(getThis()) == 0) {
//                    ToastUtil.show(getThis(), getResources().getString(R.string.toast_network_failure_default), 1000);
//                    stopLoad(false);
//                    return;
//                }
                onLoadMoreFinish();
            }
        });
        initView();
    }

    @Override
    public String getCN() {
        return getClass().getName();
    }

    protected String getTAG() {
        return getCN();
    }

    protected void loadData(String name) {
        boolean b = false;
//        if (getThis() == null) {
//            b = SharedPreferencesUtils.getBoolean(getThis(),
//                    Contact.DATA_NAME, name, true);
//        } else {
//            b = SharedPreferencesUtils.getBoolean(getThis(),
//                    Contact.DATA_NAME, name, true);
//        }
        if (b) {
            autoRefresh();
        }
    }

    public void setLineView(boolean showLine) {
        tileView.setVisibility(showLine ? View.VISIBLE : View.GONE);
    }

    protected void saveTag(String name) {
//        if (getThis() == null) {
//            SharedPreferencesUtils.saveBoolean(getThis(),
//                    Contact.DATA_NAME, name, false);
//        } else {
//            SharedPreferencesUtils.saveBoolean(getThis(),
//                    Contact.DATA_NAME, name, false);
//        }
    }

    protected void autoRefresh() {
        showLoadingDialog();
        onRefreshFinish();
    }

    protected void stopLoad(boolean b) {
        if (refreshLayout != null) {
            if (isRefresh) {
                if (b) {
                    refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
//                    refreshLayout.setRefreshTime("上次更新时间：" + FormatUtil.formatDateTime(System.currentTimeMillis(), "MM-dd HH:mm"));
                } else {
                    refreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                }
            } else {
                if (b) {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                } else {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.FAIL);
                }
            }
        }
    }

    protected void stopLoad(boolean success, boolean noData) {
        if (!noData) {
            stopLoad(success);
        } else if (refreshLayout != null) {
            if (isRefresh) {
                if (success) {
                    refreshLayout.refreshFinish(PullToRefreshLayout.NO_DATA);
//                    refreshLayout.setRefreshTime("上次更新时间：" + FormatUtil.formatDateTime(System.currentTimeMillis(), "MM-dd HH:mm"));
                } else {
                    refreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                }
            } else {
                if (success) {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.NO_DATA);
                } else {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.FAIL);
                }
            }
        }
    }

    protected abstract void initView();

    protected void setCanPullDown(boolean canPullDown) {
        refreshLayout.setCanPullDown(canPullDown);
    }

    protected void setCanPullUp(boolean canPullUp) {
        refreshLayout.setCanPullUp(canPullUp);
    }

    protected void setPullDownEnabled(boolean pullDownEnabled) {
        refreshLayout.setPullDownEnabled(pullDownEnabled);
    }

    protected void setPullUpEnabled(boolean canPullUp) {
        refreshLayout.setPullUpEnabled(canPullUp);
    }

    protected void onRecyclerViewItemClick(ViewGroup parent, View view, int position) {

    }

    protected void onRecyclerViewItemLongClick(ViewGroup parent, View view, int position) {

    }

    protected abstract void onRefreshFinish();

    protected abstract void onLoadMoreFinish();

    protected void setRefreshImage(int resId) {
        refreshLayout.setRefreshImage(resId);
    }

    protected void setLoadMoreImage(int resId) {
        refreshLayout.setLoadMoreImage(resId);
    }
}
